package xyz.xtgl.oauth2.entity;

import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityCoreVersion;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

/**
 * @author liubo
 * @created 2019-12-13 18:03
 */
@Setter
public class CustomUserDetails implements UserDetails{

    private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;

    private String password;
    private String username;
    private List<GrantedAuthority> authorities;
    private Boolean accountNonExpired;
    private Boolean accountNonLocked;
    private Boolean credentialsNonExpired;
    private Boolean enabled;

    public Object data;

    public CustomUserDetails(){
        super();
    }

    public CustomUserDetails(String username, String password){
        super();
        this.username = username;
        this.password = password;
    }

    public CustomUserDetails(String username, String password, Object data , List<GrantedAuthority> authorities){
        super();
        this.username = username;
        this.password = password;
        this.data = data;
        this.authorities = authorities;
    }
    public CustomUserDetails(String username, String password, List<GrantedAuthority> authorities){
        super();
        this.username = username;
        this.password = password;
        this.authorities = authorities;
    }
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpired == null?true:enabled;
    }

    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked == null?true:enabled;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired == null?true:enabled;
    }

    @Override
    public boolean isEnabled() {
        return enabled == null?true:enabled;
    }

    public <T> T getData() {
        return (T)data;
    }
}
